package com.stx.liupengWorks;

import java.util.Scanner;

/**
 * packageName com.stx.day0917
 *
 * @author YangYi
 * @className PigSystem
 * @date 2025/9/17
 * @description TODO
 */
public class PigSystem {
    Scanner scanner = new Scanner(System.in);
    Pig[] pigs ;
    public void meun(){
        boolean flag = true;
        while (flag){
            System.out.println("*****************");
            System.out.println("**欢迎使用养猪系统**");
            System.out.println("****1、添加小猪****");
            System.out.println("****2、修改重量****");
            System.out.println("****3、删除小猪****");
            System.out.println("****4、展示小猪****");
            System.out.println("****5、退出系统****");
            System.out.println("*****************");
            int choose = scanner.nextInt();
            switch (choose){
                //添加小猪
                case 1:{
                    addPig();
                    break;
                }
                //修改重量
                case 2:{
                    revisePig();
                    break;
                }
                //删除小猪
                case 3:{
                    deletePig();


                    break;
                }
                //展示小猪
                case 4:{
                    showPigs();
                    break;
                }
                //退出系统
                case 5:{
//                    flag = false;
                    System.exit(0);
                    break;
                }
                default:{
                    System.out.println("请输入对应的选择项数字");
                    break;
                }
            }
        }
    }

    /**
     * 添加小猪
     */
    public void addPig(){
        Pig pig = new Pig();
        System.out.println("请输入小猪的编号：");
        int num = scanner.nextInt();
        pig.setNum(num);
        System.out.println("请输入小猪的姓名：");
        String name = scanner.next();
        pig.setName(name);
        System.out.println("请输入小猪体重：");
        double weigth = scanner.nextDouble();
        pig.setWeigth(weigth);
        System.out.println("请输入小猪的年龄：");
        double age = scanner.nextDouble();
        pig.setAge(age);
        // 在addPig()方法中
        System.out.println("请输入小猪健康状况（1-健康，0-不健康）：");
        int healthInput = scanner.nextInt();
        pig.setHealth(healthInput == 1);
        for (int i = 0; i < pigs.length; i++) {
            if(pigs[i] == null){
                pigs[i] = pig;
                System.out.println("添加小猪成功");
                return;
            }
        }
        System.out.println("添加失败，已无地方存放");
    }

    /**
     * 展示小猪-通过编号进行小猪查询
     */
    public void revisePig(){
        System.out.println("请输入要修改重量的小猪编号：");
        int num=scanner.nextInt();
        for(int j=0;j<pigs.length;j++){
            boolean found=false;
            for (int i=0;i< pigs.length;i++){
                if(pigs[i]!=null&&pigs[i].getNum()==num){
                    found=true;
                    System.out.println("当前小猪体重为："+pigs[i].getWeigth());
                    System.out.println("输入新的体重：");
                    double newWeigth=scanner.nextDouble();
                    pigs[i].setWeigth(newWeigth);
                    System.out.println("体重修改成功！");

                    return;
                }

            }
        }
    }

    public  void deletePig() {
        System.out.println(("请输入要删除的小猪编号："));
        int num = scanner.nextInt();
        boolean found=false;
        for (int i= 0; i < pigs.length;i++) {
            if(pigs[i]!=null&&pigs[i].getNum()==num){
                found=true;
                pigs[i]=null;
                for (int j=i;j< pigs.length-1;j++){
                    pigs[j]=pigs[j+1];
                    if(pigs[j]==null)break;
                }
                pigs[pigs.length-1]=null;
                System.out.println("编号为"+num+"的小猪已经成功删除");
                break;
            }
        }
        if(!found){
            System.out.println("未找到编号为"+num+"的小猪！");
        }

    }
    public void showPigs(){
        System.out.println("请输入需要查询的小猪编号：");
        int num = scanner.nextInt();
        for (int i = 0; i < pigs.length; i++) {
            if(pigs[i] != null && num == pigs[i].getNum()){
                System.out.println("小猪编号："+pigs[i].getNum()+"，小猪姓名："+
                        pigs[i].getName()+"，小猪体重："+pigs[i].getWeigth()+"，小猪年龄："+
                        pigs[i].getAge()+"，健康状况："+(pigs[i].isHealth() ? "健康" : "不健康"));
            }
        }
    }
}
